package com.renu.jdependencyanalyser.freemarker;

import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.renu.jdependencyanalyser.core.JarBorderGene;
import com.renu.jdependencyanalyser.core.ProjectInfo;
import com.renu.jdependencyanalyser.data.ProjectDataCenter;
import com.renu.jdependencyanalyser.freemarker.data.DependencyData;
import com.renu.jdependencyanalyser.freemarker.data.DependencyJarData;
import com.renu.jdependencyanalyser.model.ClassModel;
import com.renu.jdependencyanalyser.model.MethodModel;
import freemarker.template.Template;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Set;

public class DependencyProcessor extends AbstractProcessor{
    private DependencyData dependencyData;

    private  MethodModel methodModel;

    private ClassModel classModel;

    public DependencyProcessor(ProjectInfo projectInfo,
                               MethodModel methodModel,
                               Set<String> dirOrIndirOuterMethodSig) {
        super();
        this.methodModel = methodModel;
        this.dependencyData = new DependencyData(projectInfo,methodModel,dirOrIndirOuterMethodSig);
    }

    public DependencyProcessor(ProjectInfo projectInfo, MethodModel methodModel) {
        super();
        this.methodModel = methodModel;
        this.dependencyData = new DependencyData(projectInfo,methodModel);

    }

    public DependencyProcessor(JarBorderGene jarBorderGene) {
        super();
        this.dependencyData = new DependencyData(jarBorderGene.getBorderJars(),jarBorderGene.getDependencyNode());
    }

    // todo
    public DependencyProcessor(ProjectInfo projectInfo, ClassModel classModel, int direction) {
        super();
        this. classModel= classModel;
        this.dependencyData = new DependencyData(projectInfo,classModel,direction);
        ProjectDataCenter.dependencyData = this.dependencyData;
    }

    @Override
    protected Object getModel() {
        HashMap<String, Object> model = new HashMap<>();
        model.put("outerIds", dependencyData.getOuterIds());
        model.put("IdRlts", dependencyData.getIdRlts());
        model.put("allLabels", dependencyData.getAllLabels());

        return model;
    }

    @Override
    protected Template getTemplate() throws IOException {
        String templateContent = UrlUtil.loadText(DependencyProcessor.class.getResource(File.separator + "template" + File.separator + "InvokeTemplate.ftl"));
        stringTemplateLoader.putTemplate("InvokeTemplate",templateContent);
        return configuration.getTemplate("InvokeTemplate");
    }

    @Override
    protected Writer getWriter() {
        return new StringWriter();
    }

}
